package com.cn.ccp.action.faculty;

import java.util.List;
import java.util.Map;

import javax.faces.context.FacesContext;

import org.apache.log4j.Logger;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.jsf.FacesContextUtils;

import com.cn.ccp.action.BaseAction;
import com.cn.ccp.bean.FacultyBean;
import com.cn.ccp.service.faculty.FacultyService;
import com.cn.ccp.util.MsgConstants;

/**
 * 学院管理action
 * @author renyongcheng
 *
 */
public class FacultyAction extends BaseAction{
	private Logger log = Logger.getLogger(FacultyAction.class);
	private FacultyService facultyService;
    private FacultyBean facultyBean;
	private List<FacultyBean> facultyList;
    private String facultyTitle,facultyName,facultyDescription;
	
    public FacultyAction(){
    	super();
    	facultyTitle = getMessages(MsgConstants.FACULTY_PAGE_TITLE);
    	facultyName = getMessages(MsgConstants.FACULTY_PAGE_LABEL_FACULTY_NAME);
    	facultyDescription = getMessages(MsgConstants.FACULTY_PAGE_LABEL_FACULTY_DES);
    	WebApplicationContext fcontextutils = FacesContextUtils
				.getWebApplicationContext(FacesContext.getCurrentInstance());
    	facultyService = (FacultyService) fcontextutils
				.getBean("facultyService");
    	getpagedetail();
    }
    
	
	public FacultyService getFacultyService() {
		return facultyService;
	}

	public List<FacultyBean> getFacultyList() {
		return facultyList;
	}

	public void setFacultyList(List<FacultyBean> facultyList) {
		this.facultyList = facultyList;
	}

	public void setFacultyService(FacultyService facultyService) {
		this.facultyService = facultyService;
	}

	public FacultyBean getFacultyBean() {
		if(facultyBean == null)
			facultyBean = new FacultyBean();
		return facultyBean;
	}

	public void setFacultyBean(FacultyBean facultyBean) {
		this.facultyBean = facultyBean;
	}
	
	public void getpagedetail() {
		setItemCount(facultyService.getCountFaculty());
		int currentpage = getCurrentPage();
		int pagesize = getPageSize();
		int start = (currentpage - 1) * pagesize;
		facultyList = facultyService.pageQueryFaculty(start, pagesize);
	}
	
	public String edit(){
		log.info("---------------------------");
		boolean flag = facultyService.facultyNameIsAlready(facultyBean.getId(), facultyBean.getFacultyName());
		if(flag){
			showMsg("facultyName",  MsgConstants.FACULTY_NAME_HAS_BEEN_USED);
			return "";
		}
		if(facultyBean.getId() == 0)
			return save();
		return update();
	}
	public String save(){
		boolean flag = facultyService.saveFaculty(facultyBean);
		if(flag){
			getpagedetail();
			return MAIN;
		}
		return "";
	}
	
	public String update(){
		boolean flag = facultyService.updateFaculty(facultyBean);
		if(flag){
			getpagedetail();
			return MAIN;
		}
		return "";
	}

	public String goUpdate(){
		Map<String, String> varMap = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap();
		String id = varMap.get("id");
		log.info("---------------------------------"+id);
		if(id != null)
			facultyBean =  (FacultyBean)facultyService.findFacultyById(Integer.parseInt(id));
		log.info("-----------------"+facultyBean);
		return UPDATE;
	}
	
	
	
	public void doDelete(){
		FacesContext context = FacesContext.getCurrentInstance();// 取得上下文
		String id = (String)context.getExternalContext().getRequestParameterMap().get("id");// 得到页面的参数
		facultyService.deleteFaculty(Integer.parseInt(id));
		getpagedetail();
		log.info("-----------------doDelete()----------------"+id);
	}

	public String getFacultyTitle() {
		return facultyTitle;
	}

	public String getFacultyName() {
		return facultyName;
	}

	public String getFacultyDescription() {
		return facultyDescription;
	}
	
}
